import {useUserStore} from "@/stores/user";
/**
 * 路由守卫声明
 */
import type {NavigationFailure, NavigationGuardNext, RouteLocationNormalized} from "vue-router";

//全局前置守卫
export const beforeEach = async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
	const userStore = useUserStore();
	const token = userStore.userToken;
	if (token) {//登录的情况下
		if (to.name === 'login') {//去登录页时,不允许
			next('/');
			return;
		}
		const userInfo = userStore.userInfo;
		//判断是否获取过用户信息
		if (!userInfo) {//没获取过有用户信息时，获取一下
			await userStore.getUser();
		}
		next();

	} else {//未登录情况
		next();
	}

}

//全局后置守卫
export const afterEach = (to: RouteLocationNormalized, from: RouteLocationNormalized, failure: void | NavigationFailure | undefined) => {

}